package com.beteng.su.betengsu.ui.fragment.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.beteng.su.betengsu.R;
import com.beteng.su.betengsu.adapter.OrderSearchAdapter;
import com.beteng.su.betengsu.base.BaseActivity;
import com.beteng.su.betengsu.bean.OrderBean;
import com.beteng.su.betengsu.net.webservice.SiteService;
import com.beteng.su.betengsu.utils.CommonUtils;
import com.beteng.su.betengsu.widget.AutoToolbar;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.SnackbarUtils;
import com.paginate.Paginate;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerView;
import czm.statebutton.StateButton;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by chenzhimin on 2017/9/6.
 */

public class OrderSearchActivity extends BaseActivity {

    @BindView(R.id.toolbar_back)
    AutoRelativeLayout mToolbarBack;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    AutoToolbar mToolbar;
    @BindView(R.id.search_order_no)
    AppCompatEditText mSearchOrderNo;
    @BindView(R.id.search_order_no_beteng)
    AppCompatEditText mSearchOrderNoBeteng;
    @BindView(R.id.search_order_no_location)
    AppCompatEditText mSearchOrderNoLocation;
    @BindView(R.id.search_order_no_user)
    AppCompatEditText mSearchOrderNoUser;
    @BindView(R.id.btn_search)
    StateButton mBtnSearch;
    @BindView(R.id.order_search_rv)
    XRecyclerView mOrderSearchRv;


    public static final String SERACH_ITEM_SINGLE_INTENT = "SERACH_ITEM_SINGLE_INTENT";
    public static final String SERACH_ITEM_NUTIL_INTENT = "SERACH_ITEM_NUTIL_INTENT";

    private Paginate mPaginate;
    private boolean isLoadingMore;


    public static void start(Context context) {
        Intent starter = new Intent(context, OrderSearchActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mToolbarTitle.setText(getTitle());
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_search;
    }


    private HashMap<String, Object> getParams() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("toArea_PurposeName", mSearchOrderNoLocation.getText().toString().trim());
        params.put("SupplierWaybillNO", mSearchOrderNo.getText().toString().trim());
        params.put("WaybillID", mSearchOrderNoBeteng.getText().toString().trim());
        params.put("ReceivePerson", mSearchOrderNoUser.getText().toString().trim());
        params.put("CurrentPage", 0);
        params.put("PageSize", 100);
        params.put("CurrentPage", 0);
        return params;
    }

    /**
     * 初始化RecycleView
     */
    private void initRecycleView() {
        CommonUtils.configRecycleView(mOrderSearchRv, new LinearLayoutManager(this));
    }




    @OnClick({R.id.toolbar_back, R.id.btn_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.btn_search:
                clickSearch();
                break;
        }
    }


    private void clickSearch(){

        showLoading();

        SiteService.getInstants().getOrders(getParams(), context)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<OrderBean>>() {
                    @Override
                    public void accept(List<OrderBean> orderBeanList) throws Exception {

                        hideLoading();

                        KeyboardUtils.hideSoftInput(OrderSearchActivity.this);

                        OrderSearchAdapter orderSearchAdapter = new OrderSearchAdapter(context);

                        orderSearchAdapter.setData(orderBeanList);


                        orderSearchAdapter.setRecItemClick(new RecyclerItemCallback<OrderBean, OrderSearchAdapter.Holder>() {
                            @Override
                            public void onItemClick(int position, OrderBean model, int tag, OrderSearchAdapter.Holder holder) {
                                super.onItemClick(position, model, tag, holder);
                                ArrayList<OrderBean> list = new ArrayList<>();
                                list.add(model);
                                Intent intent = new Intent(context, ItemDetailActivity.class);
                                intent.setAction(SERACH_ITEM_NUTIL_INTENT);
                                intent.putParcelableArrayListExtra(SERACH_ITEM_NUTIL_INTENT, list);
                                startActivity(intent);
                            }

                            @Override
                            public void onItemLongClick(int position, OrderBean model, int tag, OrderSearchAdapter.Holder holder) {
                                super.onItemLongClick(position, model, tag, holder);
                            }
                        });


                        initRecycleView();


                        mOrderSearchRv.setAdapter(orderSearchAdapter);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideLoading();
                        SnackbarUtils.with(mBtnSearch).setMessage(throwable.getMessage()).showWarning();
                    }
                });


    }
}
